Examples
This page provides practical examples to help you get started with swiftshadow. It covers all major features, including filtering, caching, rotation, and integration with popular libraries.
Basic Usage
Fetching a Proxy
To fetch a random proxy:
from swiftshadow import QuickProxy
proxy = QuickProxy()
print(proxy.as_string()) # Output: http://<ip>:<port>
Filtering Proxies
Country Filter
Filter proxies by country using two-letter ISO codes:
from swiftshadow.classes import ProxyInterface
# Fetch proxies from the US and India
swift = ProxyInterface(countries=["US", "IN"])
print(swift.get().as_string())
Protocol Filter
Filter proxies by protocol (http or https):
from swiftshadow.classes import ProxyInterface
# Fetch HTTPS proxies
swift = ProxyInterface(protocol="https")
print(swift.get().as_string())
Proxy Rotation
Manual Rotation
Manually rotate proxies using the rotate() method:
from swiftshadow.classes import ProxyInterface
swift = ProxyInterface()
# Get the first proxy
print(swift.get().as_string())
# Rotate to a new proxy
swift.rotate()
print(swift.get().as_string())
Automatic Rotation
Enable automatic rotation by setting autoRotate=True:
from swiftshadow.classes import ProxyInterface
swift = ProxyInterface(autoRotate=True)
# Each call to get() will return a new proxy
print(swift.get().as_string())
print(swift.get().as_string())
Caching
Custom Cache Folder
Specify a custom cache folder (useful for AWS Lambda):
from swiftshadow.classes import ProxyInterface
# Use the /tmp directory for caching
swift = ProxyInterface(cacheFolderPath="/tmp")
print(swift.get().as_string())
Disabling Caching
For one-off use cases, use QuickProxy:
Integration with Libraries
Using with requests
Route requests through a proxy using the requests library:
from swiftshadow import QuickProxy
from requests import get
proxy = QuickProxy()
resp = get("https://checkip.amazonaws.com", proxies=proxy.as_requests_dict())
print(resp.text) # Output: Proxy's IP address
Using with httpx
Route requests through a proxy using the httpx library:
from swiftshadow import QuickProxy
import httpx
proxy = QuickProxy()
with httpx.Client(
proxies={"http://": proxy.as_string(), "https://": proxy.as_string()}
) as client:
resp = client.get("https://checkip.amazonaws.com")
print(resp.text) # Output: Proxy's IP address
Using with aiohttp
Route requests through a proxy using the aiohttp library:
import aiohttp
import asyncio
from swiftshadow import QuickProxy
async def main():
proxy = QuickProxy()
# Example headers to mimic a real browser (e.g., Firefox)
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0"
}
async with aiohttp.ClientSession() as session:
async with session.get(
"https://checkip.amazonaws.com",
proxy=proxy.as_string(),
headers=headers
) as resp:
print(await resp.text()) # Output: Proxy's IP address
asyncio.run(main())
Advanced Usage
Combining Filters
Combine country and protocol filters for precise proxy selection:
from swiftshadow.classes import ProxyInterface
# Fetch HTTPS proxies from the US and India
swift = ProxyInterface(countries=["US", "IN"], protocol="https")
print(swift.get().as_string())
Force Cache Update
Force an update of the proxy cache:
from swiftshadow.classes import ProxyInterface
swift = ProxyInterface()
swift.update() # Force update the proxy list
print(swift.get().as_string())
For more details on classes and methods, visit the References page.